var serverDate = undefined;
var menuData = undefined;
var onclickFlag = undefined;

$(document).ready(function() {
	setBaseUrl();
	getMenuData();
	$("#myMessageModal").load("popup/promptMessage");
	$("#loadingModal").load("popup/loading");
	menuData = setInterval(getMenuData, 30000);
	setTime();
	setInterval(setTime, 1000);
	$("#right").addClass("right2");
	$("#iframe").attr("src", "costManage/reviewRecord");
	showBody();
});
function showBody() {
	$('#mainBody').show();
}
function detalisSkip(skipUrl){
	$("#iframe").attr("src", skipUrl);
	$(".middle").hide();
}
//确定退出
function sure(data){
	if (onclickFlag) {
		var result = data && data.result;
		if(result == "Success"){
			window.location.href = getRootPath() + "login.jsp";
		} else {
			showMessge("系统异常，请重试");
		}
	} else {
		var contentWindow = $("#iframe") && $("#iframe")[0] && $("#iframe")[0].contentWindow;
		if (contentWindow) {
			contentWindow.sure(data);
		} else {
			showMessge("操作异常");
		}
	}
}

function getUrl(){
	if (onclickFlag) {
		return 'logOut';
	} else {
		var contentWindow = $("#iframe") && $("#iframe")[0] && $("#iframe")[0].contentWindow;
		if (contentWindow && typeof contentWindow.getUrl === 'function') {
			return contentWindow.getUrl();
		} else {
			return undefined;
		}
	}
}

function getData(){
	if (!onclickFlag) {
		var contentWindow = $("#iframe") && $("#iframe")[0] && $("#iframe")[0].contentWindow;
		if (contentWindow && typeof contentWindow.getData === 'function') {
			return contentWindow.getData();
		} else {
			return undefined;
		}
	}
}

function checkData(){
	if (!onclickFlag) {
		var contentWindow = $("#iframe") && $("#iframe")[0] && $("#iframe")[0].contentWindow;
		if (contentWindow && typeof contentWindow.checkData === 'function') {
			return contentWindow.checkData();
		} else {
			return true;
		}
	} else {
		return true;
	}
}

function logOut(){
	onclickFlag = "logOut";
	showMessge("确定要退出吗?","model2");
}

function getMenuData() {
	$.ajax({
		type: 'post',
		url: "getMenuData",
		success: function(data) {
			if (typeof data === 'string') {
				if (data.indexOf("login") >= 0) {
					getRootPath() + "login.jsp";
				}
				return;
			}
			
			if (data) {
				clearInterval(menuData);
				serverDate = new Date(data.date).getTime();
				
				var userName = data.userName;
				$("#subBranchCompanyID").val(data.subBranchCompanyID);
				$("#teamID").val(data.teamID);
				if (userName) {
					$("#userName").html(data.userName);
				} else {
					$("#userName").html("登录名为空");
				}
			} else {
				$("#userName").html("登录名未获取");
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			$("#userName").html("登录异常");
		},
		timeout: function(data){
			$("#userName").html("登录超时");
		}
	})
}
function setTime() {
	$("#time").html(formatTime(serverDate));
	if (serverDate) {
		serverDate = serverDate + 1000;
	}
	$("#systemTime").val(serverDate);
}
function formatTime(time) {
	var now = new Date();
	if (time) {
		now = new Date(time);
	}
	var year = now.getFullYear();
	var month = now.getMonth() + 1;
	var day = ((now.getDate() < 10) ? "0" : "") + now.getDate();
	var hours = ((now.getHours() < 10) ? "0" : "") + now.getHours();
	var minutes = ((now.getMinutes() < 10) ? ":0" : ":") + now.getMinutes();
	var seconds = ((now.getSeconds() < 10) ? ":0" : ":") + now.getSeconds();
	var week = now.getDay();
	var weekName = [" 星期日 ", " 星期一 ", " 星期二 ", " 星期三 ", " 星期四 ", " 星期五 ", " 星期六 "];
	week = weekName[week];
	now = null;
	return year + "年" + month + "月" + day + "日" + week + hours + minutes + seconds;
}
function loginTimeout() {
	window.location.href = getRootPath() + "login.jsp";
}